package cn.hnbjx.prepaid.controller;

import cn.hnbjx.prepaid.pojo.Admin;
import cn.hnbjx.prepaid.service.AdminService;
import cn.hnbjx.prepaid.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    /**
     * 登录
     * @param name
     * @param password
     * @param httpSession
     * @return
     */
    @GetMapping("/login")
    public Result login(String name, String password, HttpSession httpSession) {
        Admin login = adminService.login(name, password);
        httpSession.setAttribute("adminid",login.getId());
        httpSession.setAttribute("isLogin", true);
        return Result.ok("登陆成功");
    }
    /**
     * 修改密码
     * @param id
     * @param oldPassword
     * @param newPassword
     * @return
     */
    @PostMapping("/updatePassword")
    public Result updatePassword(Integer id,String oldPassword,String newPassword){
        adminService.updatePassword(id, oldPassword, newPassword);
        return Result.ok("修改成功");
    }

}
